package com.air.antispider.stream.dataprocess.businessprocess

import com.air.antispider.stream.commom.bean.RequestType
import com.air.antispider.stream.dataprocess.constants.{BehaviorTypeEnum, FlightTypeEnum}

import scala.collection.mutable.ArrayBuffer

object GenerateLabel {

  /**
    * 数据打标签，区分国内国际查询预订
    * @param request
    * @param classifyRuleMap
    */
  def generateLabel(request: String, classifyRuleMap: Map[String, ArrayBuffer[String]]):RequestType = {
    //取出每种规则
    val nationalQueryList: ArrayBuffer[String] = classifyRuleMap.getOrElse("nationalQueryList", null)
    val nationalBookList: ArrayBuffer[String] = classifyRuleMap.getOrElse("nationalBookList", null)
    val internationalQueryList: ArrayBuffer[String] = classifyRuleMap.getOrElse("internationalQueryList", null)
    val internationalBookList: ArrayBuffer[String] = classifyRuleMap.getOrElse("internationalBookList", null)

    //规则与url匹配
    //判断是否为国内查询
    if (nationalQueryList != null) {
      for (rule <- nationalQueryList) {
        //对集合中的每条规则都进行匹配，如果成果，直接返回
        if (request.matches(rule)){
          return RequestType(FlightTypeEnum.National,BehaviorTypeEnum.Query)
        }
      }
    }
    //看是否是国内预定
    if (nationalBookList != null) {
      //能取到数据
      //遍历list集合
      for (rule <- nationalBookList) {
        //看当前的request和rule是否匹配
        if (request.matches(rule)){
          //如果匹配成功,就代表当前的请求是"这个规则"
          return RequestType(FlightTypeEnum.National, BehaviorTypeEnum.Book)
        }
      }
    }
    //看是否是国际查询
    if (internationalQueryList != null) {
      //能取到数据
      //遍历list集合
      for (rule <- internationalQueryList) {
        //看当前的request和rule是否匹配
        if (request.matches(rule)){
          //如果匹配成功,就代表当前的请求是"这个规则"
          return RequestType(FlightTypeEnum.International, BehaviorTypeEnum.Query)
        }
      }
    }
    //看是否是国际预定
    if (internationalBookList != null) {
      //能取到数据
      //遍历list集合
      for (rule <- internationalBookList) {
        //看当前的request和rule是否匹配
        if (request.matches(rule)){
          //如果匹配成功,就代表当前的请求是"这个规则"
          return RequestType(FlightTypeEnum.International, BehaviorTypeEnum.Book)
        }
      }
    }

    //如果上方4个维度全部没有匹配,返回"其它"标签
    RequestType(FlightTypeEnum.Other, BehaviorTypeEnum.Other)

  }

}
